
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 10/11/2013 12:09:45
-- Generated from EDMX file: D:\Leleco\Projetos CiT\Occurrences\OccurrencesCore\Model\Model.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Occurrences];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_UserUserProfile]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_UserUserProfile];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceCarrier]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Occurrences] DROP CONSTRAINT [FK_OccurrenceCarrier];
GO
IF OBJECT_ID(N'[dbo].[FK_CompetenceParametersUserProfile]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CompetenceParameters] DROP CONSTRAINT [FK_CompetenceParametersUserProfile];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceReasonOccurrenceTypeOccurrenceType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OccurrenceReasonOccurrenceTypes] DROP CONSTRAINT [FK_OccurrenceReasonOccurrenceTypeOccurrenceType];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceReasonOccurrenceTypeOccurrenceReason]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OccurrenceReasonOccurrenceTypes] DROP CONSTRAINT [FK_OccurrenceReasonOccurrenceTypeOccurrenceReason];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceOccurrenceType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Occurrences] DROP CONSTRAINT [FK_OccurrenceOccurrenceType];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceOccurrenceReason]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Occurrences] DROP CONSTRAINT [FK_OccurrenceOccurrenceReason];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskWorkflowTaskType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskWorkflowTaskType];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskWorkflowTaskStatus]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskWorkflowTaskStatus];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskWorkflowTaskLog]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTaskLogs] DROP CONSTRAINT [FK_WorkflowTaskWorkflowTaskLog];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskUserProfile]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskUserProfile];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskUser];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskOccurrence]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskOccurrence];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskUser1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskUser1];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskUser2]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTasks] DROP CONSTRAINT [FK_WorkflowTaskUser2];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceReasonStandardClassification]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OccurrenceReasons] DROP CONSTRAINT [FK_OccurrenceReasonStandardClassification];
GO
IF OBJECT_ID(N'[dbo].[FK_ApplicationConfigurationUserCreated]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ApplicationConfigurations] DROP CONSTRAINT [FK_ApplicationConfigurationUserCreated];
GO
IF OBJECT_ID(N'[dbo].[FK_ApplicationConfigurationUserModified]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ApplicationConfigurations] DROP CONSTRAINT [FK_ApplicationConfigurationUserModified];
GO
IF OBJECT_ID(N'[dbo].[FK_InvoiceOccurrence]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Invoices] DROP CONSTRAINT [FK_InvoiceOccurrence];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceUserCreated]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Occurrences] DROP CONSTRAINT [FK_OccurrenceUserCreated];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceUserModified]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Occurrences] DROP CONSTRAINT [FK_OccurrenceUserModified];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskTypeOccurrenceType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTaskTypes] DROP CONSTRAINT [FK_WorkflowTaskTypeOccurrenceType];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskParameterWorkflowTaskType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTaskParameters] DROP CONSTRAINT [FK_WorkflowTaskParameterWorkflowTaskType];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskParameterUserProfileExecutor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTaskParameters] DROP CONSTRAINT [FK_WorkflowTaskParameterUserProfileExecutor];
GO
IF OBJECT_ID(N'[dbo].[FK_WorkflowTaskParameterUserProfileSupervisor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WorkflowTaskParameters] DROP CONSTRAINT [FK_WorkflowTaskParameterUserProfileSupervisor];
GO
IF OBJECT_ID(N'[dbo].[FK_OccurrenceScheduledReturn_inherits_Occurrence]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Occurrences_OccurrenceScheduledReturn] DROP CONSTRAINT [FK_OccurrenceScheduledReturn_inherits_Occurrence];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[UserProfiles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserProfiles];
GO
IF OBJECT_ID(N'[dbo].[CompetenceParameters]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CompetenceParameters];
GO
IF OBJECT_ID(N'[dbo].[Occurrences]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Occurrences];
GO
IF OBJECT_ID(N'[dbo].[Carriers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Carriers];
GO
IF OBJECT_ID(N'[dbo].[OccurrenceReasons]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OccurrenceReasons];
GO
IF OBJECT_ID(N'[dbo].[DistributionCenters]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DistributionCenters];
GO
IF OBJECT_ID(N'[dbo].[OccurrenceTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OccurrenceTypes];
GO
IF OBJECT_ID(N'[dbo].[OccurrenceReasonOccurrenceTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OccurrenceReasonOccurrenceTypes];
GO
IF OBJECT_ID(N'[dbo].[WorkflowTasks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WorkflowTasks];
GO
IF OBJECT_ID(N'[dbo].[WorkflowTaskTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WorkflowTaskTypes];
GO
IF OBJECT_ID(N'[dbo].[WorkflowTaskStatus]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WorkflowTaskStatus];
GO
IF OBJECT_ID(N'[dbo].[WorkflowTaskLogs]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WorkflowTaskLogs];
GO
IF OBJECT_ID(N'[dbo].[StandardClassifications]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StandardClassifications];
GO
IF OBJECT_ID(N'[dbo].[ApplicationConfigurations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ApplicationConfigurations];
GO
IF OBJECT_ID(N'[dbo].[Invoices]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Invoices];
GO
IF OBJECT_ID(N'[dbo].[WorkflowTaskParameters]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WorkflowTaskParameters];
GO
IF OBJECT_ID(N'[dbo].[Occurrences_OccurrenceScheduledReturn]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Occurrences_OccurrenceScheduledReturn];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [Active] bit  NOT NULL,
    [UserType] nvarchar(max)  NOT NULL,
    [Login] nvarchar(max)  NOT NULL,
    [UserProfileId] int  NOT NULL
);
GO

-- Creating table 'UserProfiles'
CREATE TABLE [dbo].[UserProfiles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Acronym] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'CompetenceParameters'
CREATE TABLE [dbo].[CompetenceParameters] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UserProfileId] int  NOT NULL,
    [InitialValue] float  NOT NULL,
    [FinalValue] float  NOT NULL
);
GO

-- Creating table 'Occurrences'
CREATE TABLE [dbo].[Occurrences] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [CarrierId] int  NOT NULL,
    [OccurrenceDate] datetime  NOT NULL,
    [OccurrenceTypeId] int  NOT NULL,
    [OccurrenceReasonId] int  NOT NULL,
    [CreatedAt] datetime  NOT NULL,
    [ModifiedAt] datetime  NOT NULL,
    [CreatedBy_Id] int  NOT NULL,
    [ModifiedBy_Id] int  NOT NULL
);
GO

-- Creating table 'Carriers'
CREATE TABLE [dbo].[Carriers] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [CNPJ] nvarchar(max)  NOT NULL,
    [Address] nvarchar(max)  NOT NULL,
    [Contact] nvarchar(max)  NOT NULL,
    [ContactPhone] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'OccurrenceReasons'
CREATE TABLE [dbo].[OccurrenceReasons] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [EDICode] nvarchar(max)  NOT NULL,
    [StandardCostCenter] nvarchar(max)  NOT NULL,
    [StandardClassificationId] int  NOT NULL
);
GO

-- Creating table 'DistributionCenters'
CREATE TABLE [dbo].[DistributionCenters] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [CNPJ] nvarchar(max)  NOT NULL,
    [SAPCode] nvarchar(max)  NOT NULL,
    [Active] bit  NOT NULL
);
GO

-- Creating table 'OccurrenceTypes'
CREATE TABLE [dbo].[OccurrenceTypes] (
    [Id] int  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'OccurrenceReasonOccurrenceTypes'
CREATE TABLE [dbo].[OccurrenceReasonOccurrenceTypes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [OccurrenceTypeId] int  NOT NULL,
    [OccurrenceReasonId] int  NOT NULL,
    [Active] bit  NOT NULL
);
GO

-- Creating table 'WorkflowTasks'
CREATE TABLE [dbo].[WorkflowTasks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [TaskData] nvarchar(max)  NOT NULL,
    [DueDate] datetime  NOT NULL,
    [WorkflowTaskTypeId] int  NOT NULL,
    [WorkflowTaskStatusId] int  NOT NULL,
    [AssignedUserProfileId] int  NULL,
    [AssignedUserId] int  NULL,
    [OccurrenceId] int  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [CreatedAt] datetime  NOT NULL,
    [ModifiedAt] datetime  NOT NULL,
    [ModifiedBy_Id] int  NULL,
    [CreatedBy_Id] int  NULL
);
GO

-- Creating table 'WorkflowTaskTypes'
CREATE TABLE [dbo].[WorkflowTaskTypes] (
    [Id] int  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [OccurrenceTypeId] int  NOT NULL
);
GO

-- Creating table 'WorkflowTaskStatus'
CREATE TABLE [dbo].[WorkflowTaskStatus] (
    [Id] int  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'WorkflowTaskLogs'
CREATE TABLE [dbo].[WorkflowTaskLogs] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [WorkflowTaskId] int  NOT NULL,
    [Message] nvarchar(max)  NOT NULL,
    [AdminOnly] bit  NOT NULL
);
GO

-- Creating table 'StandardClassifications'
CREATE TABLE [dbo].[StandardClassifications] (
    [Id] int  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'ApplicationConfigurations'
CREATE TABLE [dbo].[ApplicationConfigurations] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Key] nvarchar(max)  NOT NULL,
    [Value] nvarchar(max)  NOT NULL,
    [Type] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [CreatedAt] datetime  NOT NULL,
    [ModifiedAt] datetime  NOT NULL,
    [CreatedBy_Id] int  NOT NULL,
    [ModifiedBy_Id] int  NOT NULL
);
GO

-- Creating table 'Invoices'
CREATE TABLE [dbo].[Invoices] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [OccurrenceId] int  NOT NULL,
    [Number] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'WorkflowTaskParameters'
CREATE TABLE [dbo].[WorkflowTaskParameters] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UserProfileExecutorId] int  NOT NULL,
    [UserProfileSupervisorId] int  NOT NULL,
    [BusinessDays] int  NOT NULL,
    [NofitySupervisorPercentage] int  NOT NULL,
    [WorkflowTaskType_Id] int  NOT NULL
);
GO

-- Creating table 'Occurrences_OccurrenceScheduledReturn'
CREATE TABLE [dbo].[Occurrences_OccurrenceScheduledReturn] (
    [Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserProfiles'
ALTER TABLE [dbo].[UserProfiles]
ADD CONSTRAINT [PK_UserProfiles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CompetenceParameters'
ALTER TABLE [dbo].[CompetenceParameters]
ADD CONSTRAINT [PK_CompetenceParameters]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Occurrences'
ALTER TABLE [dbo].[Occurrences]
ADD CONSTRAINT [PK_Occurrences]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Carriers'
ALTER TABLE [dbo].[Carriers]
ADD CONSTRAINT [PK_Carriers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OccurrenceReasons'
ALTER TABLE [dbo].[OccurrenceReasons]
ADD CONSTRAINT [PK_OccurrenceReasons]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'DistributionCenters'
ALTER TABLE [dbo].[DistributionCenters]
ADD CONSTRAINT [PK_DistributionCenters]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OccurrenceTypes'
ALTER TABLE [dbo].[OccurrenceTypes]
ADD CONSTRAINT [PK_OccurrenceTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OccurrenceReasonOccurrenceTypes'
ALTER TABLE [dbo].[OccurrenceReasonOccurrenceTypes]
ADD CONSTRAINT [PK_OccurrenceReasonOccurrenceTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [PK_WorkflowTasks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'WorkflowTaskTypes'
ALTER TABLE [dbo].[WorkflowTaskTypes]
ADD CONSTRAINT [PK_WorkflowTaskTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'WorkflowTaskStatus'
ALTER TABLE [dbo].[WorkflowTaskStatus]
ADD CONSTRAINT [PK_WorkflowTaskStatus]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'WorkflowTaskLogs'
ALTER TABLE [dbo].[WorkflowTaskLogs]
ADD CONSTRAINT [PK_WorkflowTaskLogs]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StandardClassifications'
ALTER TABLE [dbo].[StandardClassifications]
ADD CONSTRAINT [PK_StandardClassifications]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ApplicationConfigurations'
ALTER TABLE [dbo].[ApplicationConfigurations]
ADD CONSTRAINT [PK_ApplicationConfigurations]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Invoices'
ALTER TABLE [dbo].[Invoices]
ADD CONSTRAINT [PK_Invoices]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'WorkflowTaskParameters'
ALTER TABLE [dbo].[WorkflowTaskParameters]
ADD CONSTRAINT [PK_WorkflowTaskParameters]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Occurrences_OccurrenceScheduledReturn'
ALTER TABLE [dbo].[Occurrences_OccurrenceScheduledReturn]
ADD CONSTRAINT [PK_Occurrences_OccurrenceScheduledReturn]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [UserProfileId] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_UserUserProfile]
    FOREIGN KEY ([UserProfileId])
    REFERENCES [dbo].[UserProfiles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserUserProfile'
CREATE INDEX [IX_FK_UserUserProfile]
ON [dbo].[Users]
    ([UserProfileId]);
GO

-- Creating foreign key on [CarrierId] in table 'Occurrences'
ALTER TABLE [dbo].[Occurrences]
ADD CONSTRAINT [FK_OccurrenceCarrier]
    FOREIGN KEY ([CarrierId])
    REFERENCES [dbo].[Carriers]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceCarrier'
CREATE INDEX [IX_FK_OccurrenceCarrier]
ON [dbo].[Occurrences]
    ([CarrierId]);
GO

-- Creating foreign key on [UserProfileId] in table 'CompetenceParameters'
ALTER TABLE [dbo].[CompetenceParameters]
ADD CONSTRAINT [FK_CompetenceParametersUserProfile]
    FOREIGN KEY ([UserProfileId])
    REFERENCES [dbo].[UserProfiles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompetenceParametersUserProfile'
CREATE INDEX [IX_FK_CompetenceParametersUserProfile]
ON [dbo].[CompetenceParameters]
    ([UserProfileId]);
GO

-- Creating foreign key on [OccurrenceTypeId] in table 'OccurrenceReasonOccurrenceTypes'
ALTER TABLE [dbo].[OccurrenceReasonOccurrenceTypes]
ADD CONSTRAINT [FK_OccurrenceReasonOccurrenceTypeOccurrenceType]
    FOREIGN KEY ([OccurrenceTypeId])
    REFERENCES [dbo].[OccurrenceTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceReasonOccurrenceTypeOccurrenceType'
CREATE INDEX [IX_FK_OccurrenceReasonOccurrenceTypeOccurrenceType]
ON [dbo].[OccurrenceReasonOccurrenceTypes]
    ([OccurrenceTypeId]);
GO

-- Creating foreign key on [OccurrenceReasonId] in table 'OccurrenceReasonOccurrenceTypes'
ALTER TABLE [dbo].[OccurrenceReasonOccurrenceTypes]
ADD CONSTRAINT [FK_OccurrenceReasonOccurrenceTypeOccurrenceReason]
    FOREIGN KEY ([OccurrenceReasonId])
    REFERENCES [dbo].[OccurrenceReasons]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceReasonOccurrenceTypeOccurrenceReason'
CREATE INDEX [IX_FK_OccurrenceReasonOccurrenceTypeOccurrenceReason]
ON [dbo].[OccurrenceReasonOccurrenceTypes]
    ([OccurrenceReasonId]);
GO

-- Creating foreign key on [OccurrenceTypeId] in table 'Occurrences'
ALTER TABLE [dbo].[Occurrences]
ADD CONSTRAINT [FK_OccurrenceOccurrenceType]
    FOREIGN KEY ([OccurrenceTypeId])
    REFERENCES [dbo].[OccurrenceTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceOccurrenceType'
CREATE INDEX [IX_FK_OccurrenceOccurrenceType]
ON [dbo].[Occurrences]
    ([OccurrenceTypeId]);
GO

-- Creating foreign key on [OccurrenceReasonId] in table 'Occurrences'
ALTER TABLE [dbo].[Occurrences]
ADD CONSTRAINT [FK_OccurrenceOccurrenceReason]
    FOREIGN KEY ([OccurrenceReasonId])
    REFERENCES [dbo].[OccurrenceReasons]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceOccurrenceReason'
CREATE INDEX [IX_FK_OccurrenceOccurrenceReason]
ON [dbo].[Occurrences]
    ([OccurrenceReasonId]);
GO

-- Creating foreign key on [WorkflowTaskTypeId] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskWorkflowTaskType]
    FOREIGN KEY ([WorkflowTaskTypeId])
    REFERENCES [dbo].[WorkflowTaskTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskWorkflowTaskType'
CREATE INDEX [IX_FK_WorkflowTaskWorkflowTaskType]
ON [dbo].[WorkflowTasks]
    ([WorkflowTaskTypeId]);
GO

-- Creating foreign key on [WorkflowTaskStatusId] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskWorkflowTaskStatus]
    FOREIGN KEY ([WorkflowTaskStatusId])
    REFERENCES [dbo].[WorkflowTaskStatus]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskWorkflowTaskStatus'
CREATE INDEX [IX_FK_WorkflowTaskWorkflowTaskStatus]
ON [dbo].[WorkflowTasks]
    ([WorkflowTaskStatusId]);
GO

-- Creating foreign key on [WorkflowTaskId] in table 'WorkflowTaskLogs'
ALTER TABLE [dbo].[WorkflowTaskLogs]
ADD CONSTRAINT [FK_WorkflowTaskWorkflowTaskLog]
    FOREIGN KEY ([WorkflowTaskId])
    REFERENCES [dbo].[WorkflowTasks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskWorkflowTaskLog'
CREATE INDEX [IX_FK_WorkflowTaskWorkflowTaskLog]
ON [dbo].[WorkflowTaskLogs]
    ([WorkflowTaskId]);
GO

-- Creating foreign key on [AssignedUserProfileId] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskUserProfile]
    FOREIGN KEY ([AssignedUserProfileId])
    REFERENCES [dbo].[UserProfiles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskUserProfile'
CREATE INDEX [IX_FK_WorkflowTaskUserProfile]
ON [dbo].[WorkflowTasks]
    ([AssignedUserProfileId]);
GO

-- Creating foreign key on [AssignedUserId] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskUser]
    FOREIGN KEY ([AssignedUserId])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskUser'
CREATE INDEX [IX_FK_WorkflowTaskUser]
ON [dbo].[WorkflowTasks]
    ([AssignedUserId]);
GO

-- Creating foreign key on [OccurrenceId] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskOccurrence]
    FOREIGN KEY ([OccurrenceId])
    REFERENCES [dbo].[Occurrences]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskOccurrence'
CREATE INDEX [IX_FK_WorkflowTaskOccurrence]
ON [dbo].[WorkflowTasks]
    ([OccurrenceId]);
GO

-- Creating foreign key on [ModifiedBy_Id] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskUser1]
    FOREIGN KEY ([ModifiedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskUser1'
CREATE INDEX [IX_FK_WorkflowTaskUser1]
ON [dbo].[WorkflowTasks]
    ([ModifiedBy_Id]);
GO

-- Creating foreign key on [CreatedBy_Id] in table 'WorkflowTasks'
ALTER TABLE [dbo].[WorkflowTasks]
ADD CONSTRAINT [FK_WorkflowTaskUser2]
    FOREIGN KEY ([CreatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskUser2'
CREATE INDEX [IX_FK_WorkflowTaskUser2]
ON [dbo].[WorkflowTasks]
    ([CreatedBy_Id]);
GO

-- Creating foreign key on [StandardClassificationId] in table 'OccurrenceReasons'
ALTER TABLE [dbo].[OccurrenceReasons]
ADD CONSTRAINT [FK_OccurrenceReasonStandardClassification]
    FOREIGN KEY ([StandardClassificationId])
    REFERENCES [dbo].[StandardClassifications]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceReasonStandardClassification'
CREATE INDEX [IX_FK_OccurrenceReasonStandardClassification]
ON [dbo].[OccurrenceReasons]
    ([StandardClassificationId]);
GO

-- Creating foreign key on [CreatedBy_Id] in table 'ApplicationConfigurations'
ALTER TABLE [dbo].[ApplicationConfigurations]
ADD CONSTRAINT [FK_ApplicationConfigurationUserCreated]
    FOREIGN KEY ([CreatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ApplicationConfigurationUserCreated'
CREATE INDEX [IX_FK_ApplicationConfigurationUserCreated]
ON [dbo].[ApplicationConfigurations]
    ([CreatedBy_Id]);
GO

-- Creating foreign key on [ModifiedBy_Id] in table 'ApplicationConfigurations'
ALTER TABLE [dbo].[ApplicationConfigurations]
ADD CONSTRAINT [FK_ApplicationConfigurationUserModified]
    FOREIGN KEY ([ModifiedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ApplicationConfigurationUserModified'
CREATE INDEX [IX_FK_ApplicationConfigurationUserModified]
ON [dbo].[ApplicationConfigurations]
    ([ModifiedBy_Id]);
GO

-- Creating foreign key on [OccurrenceId] in table 'Invoices'
ALTER TABLE [dbo].[Invoices]
ADD CONSTRAINT [FK_InvoiceOccurrence]
    FOREIGN KEY ([OccurrenceId])
    REFERENCES [dbo].[Occurrences]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_InvoiceOccurrence'
CREATE INDEX [IX_FK_InvoiceOccurrence]
ON [dbo].[Invoices]
    ([OccurrenceId]);
GO

-- Creating foreign key on [CreatedBy_Id] in table 'Occurrences'
ALTER TABLE [dbo].[Occurrences]
ADD CONSTRAINT [FK_OccurrenceUserCreated]
    FOREIGN KEY ([CreatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceUserCreated'
CREATE INDEX [IX_FK_OccurrenceUserCreated]
ON [dbo].[Occurrences]
    ([CreatedBy_Id]);
GO

-- Creating foreign key on [ModifiedBy_Id] in table 'Occurrences'
ALTER TABLE [dbo].[Occurrences]
ADD CONSTRAINT [FK_OccurrenceUserModified]
    FOREIGN KEY ([ModifiedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OccurrenceUserModified'
CREATE INDEX [IX_FK_OccurrenceUserModified]
ON [dbo].[Occurrences]
    ([ModifiedBy_Id]);
GO

-- Creating foreign key on [OccurrenceTypeId] in table 'WorkflowTaskTypes'
ALTER TABLE [dbo].[WorkflowTaskTypes]
ADD CONSTRAINT [FK_WorkflowTaskTypeOccurrenceType]
    FOREIGN KEY ([OccurrenceTypeId])
    REFERENCES [dbo].[OccurrenceTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskTypeOccurrenceType'
CREATE INDEX [IX_FK_WorkflowTaskTypeOccurrenceType]
ON [dbo].[WorkflowTaskTypes]
    ([OccurrenceTypeId]);
GO

-- Creating foreign key on [WorkflowTaskType_Id] in table 'WorkflowTaskParameters'
ALTER TABLE [dbo].[WorkflowTaskParameters]
ADD CONSTRAINT [FK_WorkflowTaskParameterWorkflowTaskType]
    FOREIGN KEY ([WorkflowTaskType_Id])
    REFERENCES [dbo].[WorkflowTaskTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskParameterWorkflowTaskType'
CREATE INDEX [IX_FK_WorkflowTaskParameterWorkflowTaskType]
ON [dbo].[WorkflowTaskParameters]
    ([WorkflowTaskType_Id]);
GO

-- Creating foreign key on [UserProfileExecutorId] in table 'WorkflowTaskParameters'
ALTER TABLE [dbo].[WorkflowTaskParameters]
ADD CONSTRAINT [FK_WorkflowTaskParameterUserProfileExecutor]
    FOREIGN KEY ([UserProfileExecutorId])
    REFERENCES [dbo].[UserProfiles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskParameterUserProfileExecutor'
CREATE INDEX [IX_FK_WorkflowTaskParameterUserProfileExecutor]
ON [dbo].[WorkflowTaskParameters]
    ([UserProfileExecutorId]);
GO

-- Creating foreign key on [UserProfileSupervisorId] in table 'WorkflowTaskParameters'
ALTER TABLE [dbo].[WorkflowTaskParameters]
ADD CONSTRAINT [FK_WorkflowTaskParameterUserProfileSupervisor]
    FOREIGN KEY ([UserProfileSupervisorId])
    REFERENCES [dbo].[UserProfiles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WorkflowTaskParameterUserProfileSupervisor'
CREATE INDEX [IX_FK_WorkflowTaskParameterUserProfileSupervisor]
ON [dbo].[WorkflowTaskParameters]
    ([UserProfileSupervisorId]);
GO

-- Creating foreign key on [Id] in table 'Occurrences_OccurrenceScheduledReturn'
ALTER TABLE [dbo].[Occurrences_OccurrenceScheduledReturn]
ADD CONSTRAINT [FK_OccurrenceScheduledReturn_inherits_Occurrence]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Occurrences]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------